package com.xquant.transaction_management_system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xquant.transaction_management_system.model.PO.FinancialInstruments;
import com.xquant.transaction_management_system.model.PO.FinancialInstrumentsValuation;
import com.xquant.transaction_management_system.model.VO.SearchValvationVo;
import com.xquant.transaction_management_system.service.FinancialInstrumentsValuationService;
import com.xquant.transaction_management_system.utils.Result;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api("金融工具估值")
@RestController
@CrossOrigin
@RequestMapping("/admin")
public class FinancialInstrumentsValuationController {
    @Autowired
    private FinancialInstrumentsValuationService financialInstrumentsValuationService;
    @PostMapping("/getValuation/{page}/{limit}")
    public Result getValuation(@PathVariable long page,
                                     @PathVariable long limit,
                                     @RequestBody SearchValvationVo searchValvationVo) {
        Page<FinancialInstrumentsValuation> pageParam = new Page<>();
        pageParam.setCurrent(page-1);
        pageParam.setSize(limit);
        IPage<FinancialInstrumentsValuation> pageModel = financialInstrumentsValuationService.selectByPage(pageParam,searchValvationVo);
        return Result.ok(pageModel);
    }
    @PostMapping("/getValuationByPage/{page}/{limit}")
    public Result getValuationByPage(@PathVariable long page,
                                     @PathVariable long limit
                                     ) {
        Page<FinancialInstrumentsValuation> pageParam = new Page<>();
        pageParam.setCurrent(page-1);
        pageParam.setSize(limit);
        IPage<FinancialInstrumentsValuation> pageModel = financialInstrumentsValuationService.selectByPages(pageParam);
        return Result.ok(pageModel);
    }
    @GetMapping("/getValuationById/{id}")
    public Result getValuationById(@PathVariable long id
                               ) {
        FinancialInstrumentsValuation byId = financialInstrumentsValuationService.getById(id);
        return Result.ok(byId);
    }
    @PostMapping("/updateValuation/{id}")
    public Result updateValuation(@RequestBody FinancialInstrumentsValuation financialInstrumentsValuation
    ) {
        financialInstrumentsValuationService.updateById(financialInstrumentsValuation);
        return Result.ok();
    }

    @GetMapping("/deleteValuation/{id}")
    public Result deleteValuation(@PathVariable Long id) {
        financialInstrumentsValuationService.removeById(id);
        return Result.ok();
    }
    @GetMapping("/getByDecode/{debentureCode}")
    public Result getByDecode(@PathVariable String debentureCode) {
        QueryWrapper<FinancialInstrumentsValuation> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("debenture_code",debentureCode);
        FinancialInstrumentsValuation valuation = financialInstrumentsValuationService.getOne(queryWrapper);
        if (valuation==null){
            return Result.fail();
        }
        return Result.ok(valuation.getValuation());
    }
}
